package com.htzos.common.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletResponse;


/**
 * 多线程文件拷贝
 * 
 */
public class FileUtil {


	 public static boolean copyFile(String url, File file){
		  try {
			  byte[] btImg = getImageFromNetByUrl(url);
			  if(null != btImg && btImg.length > 0){
			   FileOutputStream fops = new FileOutputStream(file);
			   fops.write(btImg);
			   fops.flush();
			   fops.close();
			   return true;
			  }
		  } catch (Exception e) {
//		   e.printStackTrace();
		 }
		  return false;
}

	 
	 public static boolean copyFile(String comePath,String toPath){
		 try {
			 	FileInputStream input=new FileInputStream(comePath);//可替换为任何路径何和文件名
				FileOutputStream output=new FileOutputStream(toPath);//可替换为任何路径何和文件名
				int in=input.read();
				while(in!=-1){
					output.write(in);
					in=input.read();
				}
				input.close();
				output.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			 return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		 return true;
	 }
	 
	 /**
	  * 根据地址获得数据的字节流
	  * @param strUrl 网络连接地址
	  * @return
	  */
	 public static byte[] getImageFromNetByUrl(String strUrl){
	  try {
	   URL url = new URL(strUrl);
	   HttpURLConnection conn = (HttpURLConnection)url.openConnection();
	   conn.setRequestMethod("GET");
	   conn.setConnectTimeout(5 * 1000);
	   InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
	   byte[] btImg = readInputStream(inStream);//得到图片的二进制数据
	   return btImg;
	  } catch (Exception e) {
//	   e.printStackTrace();
	  }
	  return null;
	 }
	 /**
	  * 从输入流中获取数据
	  * @param inStream 输入流
	  * @return
	  * @throws Exception
	  */
	 public static byte[] readInputStream(InputStream inStream) throws Exception{
	  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
	  byte[] buffer = new byte[1024];
	  int len = 0;
	  while( (len=inStream.read(buffer)) != -1 ){
	   outStream.write(buffer, 0, len);
	  }
	  inStream.close();
	  return outStream.toByteArray();
	 }
	 
	 
	 public static void downloadLocal(HttpServletResponse response, String path) throws FileNotFoundException {
	        
	        File file = new File(path);
	        
	        // 下载本地文件
	        String fileName = file.getName();
	        
	        // 读到流中
	        InputStream inStream = new FileInputStream(file);// 文件的存放路径
	        
	        // 设置输出的格式
	        response.reset();
	        response.setContentType("bin");
	        response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
	        // 循环取出流中的数据
	        byte[] b = new byte[100];
	        int len;
	        try {
	            while ((len = inStream.read(b)) > 0)
	                response.getOutputStream().write(b, 0, len);
	            inStream.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
}